home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9869 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.8 KB  |  73 lines

  1. Path: rain.fr!world-net!usenet
  2. From: Frederic LACHASSE <lachass@worldnet.fr>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: [Q] Simple question about constructors
  5. Date: Mon, 04 Mar 1996 22:00:17 +0000
  6. Organization: World-Net information exchange, Internet provider.
  7. Message-ID: <VA.00000054.007dde45@fred>
  8. References: <4ha7d2$quc@cloner4.netcom.com>
  9. Reply-To: lachass@worldnet.fr
  10. NNTP-Posting-Host: client77.sct.fr
  11. X-Newsreader: Virtual Access by Ashmount Research Ltd, http://www.ashmount.com
  12.  
  13. In article <4ha7d2$quc@cloner4.netcom.com>, stefmit@ix.netcom.com wrote:
  14. > Trying to work/learn C++ from an old book, I got to a point where my 
  15. > understanding stops: picking a sample constructor from another much 
  16. > newer source, I came across a syntax in the form:
  17. > class_name :: class_name (arguments) : data_member1 (argument1), data_member2 
  18. > (argument2) {}
  19. > The portion I don't know the meaning of (though I assume is kind of 
  20. > intialization list, that would've appeared otherwise within {}, as far as I 
  21. > learned from my book), is the portion after ":".
  22. > I would kindly request some elaboration on this, especially concerning a 
  23. > statement in the source where I found this, similar to: "this is the only way 
  24. > the initialization can be done in some cases (!!!)". Does this mean I am not 
  25. > always able to define a constructor the way I've learned so far (in its {} 
  26. > body)?
  27.  
  28. Good habits are to initialize objects before using them. When constructing an 
  29. object (i.e. in a constructor), you must first initialize first every base 
  30. class and every member data of your object. For some, a default constructor 
  31. (that is a constructor without parameters) may be used implicitely, so you 
  32. don't need to do anything. Others may not be so lucky and you must then call 
  33. explicitely one constructor with parameters. Such is the case for const member 
  34. data and references. Finally, it may be more efficient for other objects to 
  35. initialize them with a full constructor than calling first the default one then 
  36. assigning value to the object.
  37.  
  38. Examples of code:
  39.  
  40. class int_vector
  41. {
  42.   const int size;
  43.   int *values;
  44. public:
  45.   int_vector(int s);
  46. };
  47.   
  48. int_vector::int_vector(int s)
  49. : size(s)    // initialization list: size can only be initialized here
  50.              // because it's a const int
  51.   values = new int[s]; // this could be done in the init list too
  52.                        // with "values(new int[s])" but it's not mandatory
  53. }
  54.  
  55. // now a 3D points
  56. class 3Dpoint : public int_vector
  57. {
  58. public:
  59.   3Dpoint() : int_vector(3) {} // here the base class (int_vector) has no
  60.                                // default constructor, so we need to call one
  61.                                // explicitely
  62. }
  63.  
  64. This syntax is only necessary (and only allowed) in constructors.
  65. I hope this'll help.
  66.  
  67.  Frederic LACHASSE (ECP 86)
  68.  CompuServe: 100530,2005
  69.  Internet: lachass@worldnet.fr
  70.  
  71.